<?php

/**
 * Created by Atom.
 * User: qiliping
 * Date: 2017/2/14
 * Time: 19:21
 * 学校model
 */
class Schoolo2o_model extends CI_Model
{
  /**
   * 获取微信公众号信息
   * @param  [type] $school_id [description]
   * @return [type]            [description]
   */
  public function get_wx_mp($school_id){
    $sql = "SELECT * FROM {$this->db->dbprefix}o2o_school_wx_config WHERE school_id = {$school_id} limit 1";
    return $this->db->query($sql)->row_array();
  }

  function update_wx_app_secret($school_id,$app_secret){
    //修改
      $wx = array(
        'app_secret' => $app_secret,
        'update_time' => time()
        );
      $this->db->update('o2o_school_wx_config',$wx,array('school_id' => $school_id ));
  }

  /**
   * 绑定网校微信公众号
   * @param  [type] $school_id            [description]
   * @param  [type] $appid                [description]
   * @param  [type] $access_token         [description]
   * @param  [type] $refresh_token        [description]
   * @param  [type] $expires_in           [description]
   * @param  [type] $authorized_func_info [description]
   * @return [type]                       [description]
   */
  public function bind_wx_mp($school_id,$appid,$access_token,$refresh_token,$expires_in,$authorized_func_info)
  {
    $sql = "SELECT count(0) as exist_count FROM {$this->db->dbprefix}o2o_school_wx_config WHERE school_id = {$school_id} ";
    $result = $this->db->query($sql)->row_array();
    if($result['exist_count']==0){
      //新增
      $wx = array(
        'school_id' => $school_id, 
        'app_id' => $appid,
        'access_token' => $access_token,
        'access_token_time' => time(),
        'refresh_token' => $refresh_token,
        'expires_in' => $expires_in,
        'func_info' => $authorized_func_info,
        'add_time' => time()
        );
      $this->db->insert('o2o_school_wx_config',$wx);

    }else{
      //修改
      $wx = array(
        'app_id' => $appid,
        'access_token' => $access_token,
        'access_token_time' => time(),
        'refresh_token' => $refresh_token,
        'expires_in' => $expires_in,
        'func_info' => $authorized_func_info,
        'update_time' => time()
        );
      $this->db->update('o2o_school_wx_config',$wx,array('school_id' => $school_id ));
    }
    return 1;
  }

  /**
   * 更新微信公众号信息
   * @param  [type] $school_id          [description]
   * @param  [type] $app_name           [description]
   * @param  [type] $app_head_img       [description]
   * @param  [type] $app_user_name      [description]
   * @param  [type] $app_type           [description]
   * @param  [type] $app_verify_type    [description]
   * @param  [type] $app_principal_name [description]
   * @param  [type] $app_alias          [description]
   * @param  [type] $app_qrcode_url     [description]
   * @return [type]                     [description]
   */
  function update_wxopen_info($school_id,$app_name,$app_head_img,$app_user_name,$app_type,$app_verify_type,$app_principal_name,$app_alias,$app_qrcode_url){

    $sql = "SELECT count(0) as exist_count FROM {$this->db->dbprefix}o2o_school_wx_config WHERE school_id = {$school_id} ";
    $result = $this->db->query($sql)->row_array();
    if($result['exist_count']==1){
      $wx = array(
        'app_name' => $app_name, 
        'app_head_img' => $app_head_img, 
        'app_user_name' => $app_user_name, 
        'app_type' => $app_type, 
        'app_verify_type' => $app_verify_type, 
        'app_principal_name' => $app_principal_name, 
        'app_alias' => $app_alias, 
        'app_qrcode_url' => $app_qrcode_url, 
        'update_time' => time()
        );

      $this->db->update('o2o_school_wx_config',$wx,array('school_id' => $school_id ));
    }
    return 1;
  }
  

  /**
   * 获取学校信息
   * @param  [type] $school_id [description]
   * @return [type]            [description]
   */
  public function get_school_by_id($school_id)
  {
    $sql = "SELECT * FROM {$this->db->dbprefix}o2o_school where id = {$school_id} ";
    return $this->db->query($sql)->row_array();
  }

  /**
   * 获取校区
   * @param  [type] $school_id [description]
   * @return [type]            [description]
   */
  public function get_school_campus($school_id)
  {
    $sql = "SELECT * FROM {$this->db->dbprefix}o2o_school_campus where school_id = {$school_id} ";
    return $this->db->query($sql)->result_array();
  }

  /**
   * 获取校区info
   * 
   * @param  [type] $id [description]
   * @return [type]     [description]
   */
  function get_school_campus_by_id($id){
    $sql = "SELECT * FROM {$this->db->dbprefix}o2o_school_campus where id = {$id} limit 1 ";
    return $this->db->query($sql)->row_array();
  }

  /**
   * 删除校区
   * @param  [type] $id        [description]
   * @param  [type] $school_id [description]
   * @return [type]            [description]
   */
  public function delete_campus($id,$school_id)
  {
    $sql = "SELECT * FROM {$this->db->dbprefix}o2o_school_campus where id = {$id} limit 1 ";
    $campus = $this->db->query($sql)->row_array();
    if($campus){
      if ($campus['school_id']!=$school_id) {
        return -1;
      }
      return $this->db->delete('o2o_school_campus',array('id' => $id ));
    }else{
      return -1;
    }
  }
  /**
   * 添加学校校区
   * @param [type] $data      [description]
   * @param [type] $school_id [description]
   */
  public function add_school_campus($data,$school_id)
  {
    $campus = array(
      'name' => $data['name'],
      'school_id' => $school_id,
      'campus_school_id' => 0,
      'address' => $data['address'],
      'tel' => $data['tel'],
      'schoolmaster' => $data['schoolmaster'],
      'add_time' => time(),
    );
    return $this->db->insert('o2o_school_campus',$campus);
  }

  /**
   * 修改校区
   * @param  [type] $id   [description]
   * @param  [type] $data [description]
   * @return [type]       [description]
   */
  public function edit_school_campus($id,$data)
  {
     $campus = array(
      'name' => $data['name'],
      'address' => $data['address'],
      'tel' => $data['tel'],
      'schoolmaster' => $data['schoolmaster'],
    );
    return $this->db->update('o2o_school_campus',$campus, array('id' => $id ));
  }

  function get_school_agent($limit = 0, $offset = 10, $conditions = array()){
    $where = " WHERE agent_id='{$this->user_data['agent']}' ";
    if(!empty($conditions['school_id'])){
        $where .= " AND id = '{$conditions['school_id']}'";
    }
    $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->db->dbprefix}o2o_school $where LIMIT $limit, $offset";
    $result = $this->db->query($sql)->result_array();
    $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
    return array("count" => $row_num['row_num'], "result" => $result);
  }

  function get_school_angent_list(){
    $sql = "SELECT id, school_name FROM {$this->db->dbprefix}o2o_school WHERE agent_id='{$this->user_data['agent']}'";
    $result = $this->db->query($sql)->result_array();
    return $result;
  }
}
